package MyApp::Model::Vpnxml;
use strict;
use XML::LibXML;
use warnings;

use utf8;

my ($doc,$root);

#构造器
sub new {
    my $class = shift;
    my $filename = shift;

    # load
    my $prsr = XML::LibXML->new();
    #$prsr->keep_blanks(0);
    $doc = $prsr->parse_file("$filename");
    $root = $doc->documentElement();

    my $self = {};

    bless $self,$class;

    return $self;
}

#增加MAC地址
sub addmacaddress {
    my $self = shift;
    my $macaddress = shift;
    my $rulegroup = shift || "终端计算机MAC合规检查";

    
    #my $root = $doc->documentElement();

    my @policyname = $root->getElementsByTagName('policy-name');
    
    my $policy;

    #查找相应的policy
    foreach my $name (@policyname) {
        
        my $string = $name->textContent;
        #print $string."\n";
        next if $string !~ /$rulegroup/;
        $policy = $name->parentNode;
        #print $policy->nodeName;
    }

    my @macaddressrules = $policy->getElementsByTagName('mac-address-rule');
    my $last = $#macaddressrules;
    my $macaddressrule = $macaddressrules[$last];
    

    foreach my $mac (@$macaddress) {
        
        my @macaddresses = $macaddressrule->getElementsByTagName('mac-addresses');
        my $maclast = $#macaddresses;
        $mac = $mac->[0]; #当传入的时mojo::mysql的$result->arrays时
        if ($maclast < 491) {
            my $node = $macaddressrule->addNewChild('','mac-addresses');
            $node->appendTextNode("$mac");
        }
        else {
            my $node01 = $macaddressrule->parentNode;
            my $node02 = $node01->addNewChild('','mac-address-rule');
            my $node03 = $node02->addNewChild('','rule-name');
            my $namestring = "mac".time();
            $node03->appendTextNode("$namestring");
            $node03 = $node02->addNewChild('','requirement');
            $node03->appendTextNode('required');
            $node03 = $node02->addNewChild('','mac-addresses');
            $node03->appendTextNode("$mac");
            $macaddressrule = $node02;
        }
    }

}

#删除MAC地址
sub deletemacaddress {
    my $self = shift;
    my $macaddress = shift;

    #my $root = $doc->documentElement();
    
    my @macaddresses = $root->getElementsByTagName('mac-addresses');

    foreach my $mac (@$macaddress) {
        $mac = $mac->[0];  #当传入的时mojo::mysql的$result->arrays时
        foreach my $macchild (@macaddresses) {
            my $string = $macchild->textContent;
            next if $string !~ /$mac/;
            $macchild->parentNode->removeChild($macchild);
        }
    }
}

#增加账户
sub createuser {
    my $self = shift;
    my ($group,$user,$pass,$fullname) = @_;
    my ($usersnode,$users);

    my @nodes = $root->getElementsByTagName('name');

    foreach my $node (@nodes) {
        my $string = $node->textContent;
        next if $string !~ /$group/;
        $usersnode = $node->parentNode;
    }
    
    my @users = $usersnode->getElementsByTagName('users');
    $users = $users[0];

    my $newuser = $users->addNewChild('','user');
    my $newusernode = $newuser->addNewChild('','username');
    $newusernode->appendTextNode("$user");
    $newusernode = $newuser->addNewChild('','password-cleartext');
    $newusernode->appendTextNode("$pass");
    $newusernode = $newuser->addNewChild('','fullname');
    $newusernode->appendTextNode("$fullname");
    $newusernode = $newuser->addNewChild('','one-time-use');
    $newusernode->appendTextNode("false");
    $newusernode = $newuser->addNewChild('','console-access');
    $newusernode->appendTextNode("false");
    $newusernode = $newuser->addNewChild('','enabled');
    $newusernode->appendTextNode("true");
    $newusernode = $newuser->addNewChild('','change-password-at-signin');
    $newusernode->appendTextNode("true");

}

#删除账户
sub deleteuser {
    my $self = shift;
    my $username = shift;

    my @nodes = $root->getElementsByTagName('username');

    foreach my $node (@nodes) {
        next if $node->textContent !~ /$username/i;
        my $parent = $node->parentNode;
        $parent->setAttribute('xc:operation','delete');
    }
}

#重置密码
sub repassword {
    my $self = shift;
    my ($username,$pass) = @_;
    
    my @nodes = $root->getElementsByTagName('username');

    foreach my $node (@nodes) {
        next if $node->textContent !~ /$username/i;
        my $parent = $node->parentNode;
        my @enpassnode = $parent->getElementsByTagName('password-encrypted');
        my $rmpassnode = $enpassnode[0];
        $parent->removeChild($rmpassnode);
        my $newpassnode = $parent->addNewChild('','password-cleartext');
        $newpassnode->appendTextNode("$pass");

        #设置下次登录时要求修改密码
        @enpassnode = $parent->getElementsByTagName('change-password-at-signin');
        $rmpassnode = $enpassnode[0];
        $parent->removeChild($rmpassnode);
        $newpassnode = $parent->addNewChild('','change-password-at-signin');
        $newpassnode->appendTextNode('true');
    }

}


sub out {
    my $self = shift;
    my $filename = shift || 'out.xml';
    open my $out, '>', "$filename";
    my $str = $doc->toString();
    #print $out $doc->toString();
    print $out $str;
    close $out;
}

1;
